package net.yb.service.impl;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import net.yb.dao.UserDao;
import net.yb.entity.User;
import net.yb.exception.UserExistsException;
import net.yb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 业务层
 */
// @Component
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserDao userDao;

    @Override
    public void test() {
        userDao.test();
        System.out.println("执行了UserServiceImpl........");
    }

    @Override
    public User login(String username, String password) {
        userDao.selectByUsernameAndPassword(username, password);

        // int i = 5/0;

        User user = new User();
        user.setId(1001);
        user.setUsername(username);
        user.setPassword(password);
        user.setAge(18);
        user.setSex("男");
        user.setHeight(1.78);
        user.setBirthday(new Date());

        return user;
    }

    @Override
    public void register(User user) {
        /**
         * 判断用户是否已存在
         */
        if(user.getUsername().equals("tom")){
            throw new UserExistsException("用户已存在！");
        }

        userDao.insert(user);
    }
}
